import { GoodsItem } from "../common/GoodsItem";
import { GlobalClass } from "../../../../com/mui/core/GlobalClass";
import { PackConst } from "../../control/data/PackConst";
import { Dispatcher } from "../../../mvc/core/Dispatcher";
import { EventName } from "../../mvc/EventName";
import { ItemMenuRegister } from "../common/menu/ItemMenuRegister";
import { DataEvent } from "../../events/DataEvent";
import { Cache } from "../../cache/Cache";
import { ShopOperType } from "../shop/ShopOperType";
import { ItemMenuConst } from "../common/menu/ItemMenuConst";
import { GameController } from "../../mvc/GameController";
import { ItemsUtil } from "../shortcuts/item/ItemsUtil";
import { ECategory } from "../../../../Message/Public/ECategory";
import { EProp } from "../../../../Message/Public/EProp";
import { ItemData } from "../../resource/info/ItemData";
import { FilterConst } from "../../../component/gconst/FilterConst";
import { DragManager } from "../../../../com/mui/manager/DragManager";
import { GameManager } from "../../manager/GameManager";
import { ModuleType } from "../common/ModuleType";
import { ItemEffectType } from "../../rules/ItemEffectType";
import { ObjectPool } from "../../../../com/gengine/utils/pools/ObjectPool";
import { ListData } from "../../../../fl/controls/listClasses/ListData";
import { CDBackPackItem } from "../../cache/CDItem/CDBackPackItem";
type int = number;
//class PackItem
    
    export  class PackItem extends GoodsItem
    {
        constructor()
        {
            super();
            isShowLock = true;
            this.bgBmp = GlobalClass.getBitmap("PackItemBg");
            _bitmap.x = 3;
            _bitmap.y = 3;
            var loc1;
            lockedEffect.y = loc1 = 1;
            lockedEffect.x = loc1;
            usedEffect.y = loc1 = 1;
            usedEffect.x = loc1;
            notMeetCareerEffect.y = loc1 = 1;
            notMeetCareerEffect.x = loc1;
            this.initListener();
            this.posType = PackConst.posType;
            return;
        }

        protected /* override */ set isBind(arg1: Boolean)
        {
            super.isBind = arg1;
            if (lockedIcon) 
            {
                var loc1;
                lockedIcon.y = loc1 = 2;
                lockedIcon.x = loc1;
            }
            return;
        }

        protected /* override */ resetBitmapSize(): void
        {
            if (_bitmap) 
            {
                _bitmap.width = _width - 4;
                _bitmap.height = _height - 4;
            }
            return;
        }

        /* internal  */initListener(): void
        {
            this.addEventListener(flash.events.MouseEvent.CLICK, this.clickHandler, false, 9999);
            this.addEventListener(flash.events.MouseEvent.DOUBLE_CLICK, this.fastOperate);
            Dispatcher.addEventListener(EventName.ItemUse, this.itemUseing);
            return;
        }

        /* internal  */clickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc1=null;
            if (this.locked || this.used || !this.itemData) 
            {
                arg1.stopImmediatePropagation();
                ItemMenuRegister.hideOpList();
            }
            else if (arg1.ctrlKey) 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.ChatShowItem, itemData));
                arg1.stopImmediatePropagation();
                ItemMenuRegister.hideOpList();
            }
            else if (Cache.instance.shop.operType != ShopOperType.SALE) 
            {
                if (Cache.instance.shop.operType != ShopOperType.FIX) 
                {
                    loc1 = ItemMenuConst.getEnabeldAttri(ItemMenuConst.getDataProvider(itemData), itemData);
                    if (Cache.instance.trade.isTrading) 
                    {
                        Dispatcher.dispatchEvent(new DataEvent(EventName.Trade_PutInItem, itemData));
                        arg1.stopImmediatePropagation();
                        ItemMenuRegister.hideOpList();
                    }
                    else if (GameController.market.isSellWindowOpen() || GameController.market.isSeekBuyWindowOpen()) 
                    {
                        if (!ItemsUtil.isBind(itemData)) 
                        {
                            Dispatcher.dispatchEvent(new DataEvent(EventName.BackPackClickToMarket, itemData));
                            arg1.stopImmediatePropagation();
                            ItemMenuRegister.hideOpList();
                        }
                    }
                    else if (!GameController.stall.view.isHide && GameController.stall.isSelf) 
                    {
                        Dispatcher.dispatchEvent(new DataEvent(EventName.Stall_PutInItem, itemData));
                        arg1.stopImmediatePropagation();
                        ItemMenuRegister.hideOpList();
                    }
                    else if (GameController.wareHouse.view.isHide) 
                    {
                        if (GameController.guildWarehouse.isShow()) 
                        {
                            this.fastMoveToGuildWareHouse(arg1.target as PackItem);
                            arg1.stopImmediatePropagation();
                            ItemMenuRegister.hideOpList();
                        }
                        else if (!GameController.player.view.isHide && (ItemsUtil.isEquip(itemData) || ItemsUtil.isMount(itemData))) 
                        {
                            ItemMenuConst.opearte(loc1.getItemAt(0)["label"], itemData);
                            arg1.stopImmediatePropagation();
                            ItemMenuRegister.hideOpList();
                        }
                    }
                    else 
                    {
                        this.fastMoveToWareHouse(arg1.target as PackItem, 0);
                        arg1.stopImmediatePropagation();
                        ItemMenuRegister.hideOpList();
                    }
                }
                else 
                {
                    arg1.stopImmediatePropagation();
                    ItemMenuRegister.hideOpList();
                    if (itemData.itemInfo.item.category == ECategory._ECategoryEquip) 
                    {
                        Dispatcher.dispatchEvent(new DataEvent(EventName.ShopFixEquipment, {"posType": itemData.posType, "itemData": itemData}));
                    }
                }
            }
            else 
            {
                arg1.stopImmediatePropagation();
                ItemMenuRegister.hideOpList();
                Dispatcher.dispatchEvent(new DataEvent(EventName.ShopSellItem, itemData.uid));
            }
            return;
        }

        /* internal  */fastOperate(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc1=null;
            var loc2=0;
            var loc3=0;
            if (Cache.instance.shop.operType == ShopOperType.SALE || Cache.instance.shop.operType == ShopOperType.FIX) 
            {
                return;
            }
            if (itemData) 
            {
                loc1 = ItemMenuConst.getEnabeldAttri(ItemMenuConst.getDataProvider(itemData), itemData);
                if (!(Cache.instance.trade.isTrading || GameController.market.isSellWindowOpen() || GameController.market.isSeekBuyWindowOpen() || !GameController.stall.view.isHide && GameController.stall.isSelf || !GameController.wareHouse.view.isHide || GameController.guildWarehouse.isShow() || !GameController.player.view.isHide && (ItemsUtil.isEquip(itemData) || ItemsUtil.isMount(itemData)))) 
                {
                    loc2 = itemData.itemInfo.item.category;
                    loc3 = itemData.itemInfo.item.type;
                    if (loc2 == ECategory._ECategoryProp && loc3 == EProp._EPropPackage) 
                    {
                        this.fastUsePackage(itemData);
                    }
                    else if (loc1.length > 0) 
                    {
                        ItemMenuConst.opearte(loc1.getItemAt(0)["label"], itemData);
                    }
                }
            }
            ItemMenuRegister.hideOpList();
            return;
        }

        /* internal  */fastMoveToWareHouse(arg1: PackItem, arg2: int): void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.Fast_Move_BackPackItem_ToWareHouse, arg1));
            return;
        }

        /* internal  */fastMoveToGuildWareHouse(arg1: PackItem): void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.GuildWarehouseContri, arg1.itemData));
            return;
        }

        /* internal  */fastUsePackage(arg1: ItemData): void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.Fast_USE_BackPackExtend, arg1));
            return;
        }

        public /* override */ set selected(arg1: Boolean)
        {
            super.selected = arg1;
            if (arg1 && itemData) 
            {
                this.filters = [FilterConst.itemChooseFilter];
            }
            else 
            {
                this.filters = [];
            }
            return;
        }

        protected /* override */ onMouseDown(arg1: Object /* flash.events.MouseEvent */): void
        {
            if (itemData && !_locked && !_used) 
            {
                DragManager.instance.startDragItem(this, bitmapdata);
            }
            else if (!this.isOpened) 
            {
                if (!Cache.instance.pack.backPackExtendPackCache.isFullBags()) 
                {
                    GameManager.instance.popupWindow(ModuleType.WareHouseBuyItem);
                }
            }
            return;
        }

        public /* override */ set data(arg1: Object)
        {
            var loc1=null;
            var loc2=null;
            super.data = arg1;
            this.isOpened = arg1["isOpened"];
            this.selected = arg1["selected"];
            if (arg1.hasOwnProperty("itemData")) 
            {
                this.itemData = arg1["itemData"];
                loc1 = Cache.instance.timeCdCache.getCdTime(itemData);
                if (loc1) 
                {
                    if (loc1.isCoolDown) 
                    {
                        this.playEffect(loc1);
                    }
                    else 
                    {
                        freezingEffect.clearEffect();
                    }
                }
                else 
                {
                    freezingEffect.clearEffect();
                }
            }
            else 
            {
                this.itemData = null;
                freezingEffect.clearEffect();
            }
            this.locked = arg1["locked"];
            this.used = arg1["used"];
            this.meetCareer = arg1["meetCareer"];
            if (arg1["effect"]) 
            {
                removeEffect();
                var loc3=arg1["effect"];
                switch (loc3) 
                {
                    case ItemEffectType.LightMask:
                    {
                        loc2 = ObjectPool.getObject(LightMask);
                        loc2.transform.matrix = new Object /* flash.geom.Matrix */();
                        loc2.x = 0;
                        loc2.y = 1;
                        loc2.scaleX = 46 / 42.5;
                        loc2.scaleY = 46 / 42.5;
                        effectLayer.addChild(loc2);
                        break;
                    }
                    default:
                    {
                        break;
                    }
                }
            }
            else 
            {
                removeEffect();
            }
            return;
        }

        public /* override */ set itemData(arg1: ItemData)
        {
            super.itemData = arg1;
            if (this.itemData) 
            {
                ItemMenuRegister.register(this, this.itemData);
            }
            else 
            {
                ItemMenuRegister.unRegister(this);
            }
            return;
        }

        public /* override */ set listData(arg1: ListData)
        {
            super.listData = arg1;
            this.posIndex = listData.index + 1;
            return;
        }

        /* internal  */changeUsingItemPos(): void
        {
            return;
        }

        /* internal  */itemUseing(arg1: DataEvent): void
        {
            this.playEffect(arg1.data as CDBackPackItem);
            return;
        }

        /* internal  */playEffect(arg1: CDBackPackItem): void
        {
            if (itemData && itemData.itemAmount > 0) 
            {
                if (freezingEffect.isCDPlaying == false && arg1.isCanCd(itemData)) 
                {
                    freezingEffect.cdTime = arg1;
                }
            }
            else if (freezingEffect.isCDPlaying) 
            {
                freezingEffect.clearEffect();
            }
            return;
        }

        /* internal */ /* var */isOpened: Boolean=true;

        /* internal */ /* var */bgBmp: Object /* flash.display.Bitmap */;

        public /* var */isFlashing: Boolean;

        /* internal */ /* var */lightMask: LightMask;
    }
